package com.kbbywyclient.util;

import android.app.Activity;
import android.net.Uri;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

import com.kbbywyclient.widget.BottomPopupOption;

import java.util.List;

import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.model.PhotoInfo;

/**
 * Description:
 * Created by FHW
 * on 2017/5/5
 */

public class WebChromeClientUtil extends WebChromeClient {

    private ValueCallback<Uri> mUploadMessage;
    private ValueCallback<Uri[]> mUploadCallbackAboveL;
    public static int REQ_CHOOSE = 1;
    //    private BottomPopupOption bottomPopupOption;
    private Activity mContext;

    public WebChromeClientUtil(Activity context) {
        this.mContext = context;
    }

    //3.0++
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
        openFileChooserImpl(uploadMsg);
    }

    //3.0--
    public void openFileChooser(ValueCallback<Uri> uploadMsg) {
        openFileChooserImpl(uploadMsg);
    }

    //4.1
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
        openFileChooserImpl(uploadMsg);
    }

    //5.0 +
    @Override
    public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
        openFileChooserImplForAndroid5(filePathCallback);
        return true;
    }

    //调用///////////////////////////////////////////////////////////////

    //5.0以上
    private void openFileChooserImplForAndroid5(ValueCallback<Uri[]> uploadMsg) {
        mUploadCallbackAboveL = uploadMsg;
        dispatchTakePictureIntent();
    }

    //5.0以下的掉用
    private void openFileChooserImpl(ValueCallback<Uri> uploadMsg) {
        mUploadMessage = uploadMsg;
        dispatchTakePictureIntent();
    }

    //拍照
    private void dispatchTakePictureIntent() {
        selectImgDialog();
    }

    private void selectImgDialog() {
        BottomPopupOption bottomPopupOption = new BottomPopupOption(mContext);
        bottomPopupOption.setItemText("拍 照", "手机相册");
        bottomPopupOption.setItemClickListener(position -> {
            switch (position) {
                case 0://拍 照
//                    GalleryFinalUtil.openCamera(mOnHanlderResultCallback);
                    break;
                case 1://手机相册
                    GalleryFinalUtil.openGallerySingle(mOnHanlderResultCallback);
//                    Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT);
//                    String IMAGE_UNSPECIFIED = "image/*";
//                    innerIntent.setType(IMAGE_UNSPECIFIED); // 查看类型
//                    Intent wrapperIntent = Intent.createChooser(innerIntent, null);
//                    mContext.startActivityForResult(wrapperIntent, REQ_CHOOSE);
                    break;
            }
            bottomPopupOption.dismiss();
        });
        bottomPopupOption.showPopupWindow();
    }

    private GalleryFinal.OnHanlderResultCallback mOnHanlderResultCallback = new GalleryFinal.OnHanlderResultCallback() {
        @Override
        public void onHanlderSuccess(int reqeustCode, List<PhotoInfo> resultList) {
            Uri result = Uri.parse("content:/" + resultList.get(0).getPhotoPath());
            if (mUploadCallbackAboveL != null) {
                mUploadCallbackAboveL.onReceiveValue(new Uri[]{result});
                mUploadCallbackAboveL = null;
            } else if (mUploadMessage != null) {
                mUploadMessage.onReceiveValue(result);
                mUploadMessage = null;
            }
        }

        @Override
        public void onHanlderFailure(int requestCode, String errorMsg) {

        }
    };

}
